﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class Subject : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSystemCreated;
        protected HtmlForm F9021;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblRemark;
        protected Label lblSubjectID;
        protected Label lblSubjectName;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtRemark;
        protected TextBox txtSubjectID;
        protected TextBox txtSubjectName;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtSubjectID.Text = "";
            this.txtSubjectID.Enabled = true;
            base.DataEntityViewState = null;
            this.hdnFlag.Value = "";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.chkSystemCreated.Checked)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9120_007");
            }
            else if (!string.IsNullOrEmpty(this.txtSubjectID.Text.Trim()))
            {
                try
                {
                    List<string> ContractIDS = new List<string> {
                        this.txtSubjectID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(ContractIDS, "SUBJECT") + base.GetGlobalMsgResource("F9120_008");
                    base.DataEntityViewState = null;
                    this.btnCopy.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9120_009") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("SubjectQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("SubjectList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("Subject.aspx?from={0}", base.UrlEncode(this.hdnFrom.Value.Trim())), false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSubjectName.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9120_002");
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateSubjectInfo();
            }
            else
            {
                this.InsertSubjectInfo();
            }
        }

        private void InsertSubjectInfo()
        {
            try
            {
                SubjectInfo subjectInfo = new SubjectInfo {
                    SubjectID = this.txtSubjectID.Text,
                    SubjectName = this.txtSubjectName.Text,
                    Remark = this.txtRemark.Text,
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    SystemCreated = this.chkSystemCreated.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UpdatedBy
                };
                string strSubjectID = string.Empty;
                strSubjectID = BaseInfoServiceClientProxy.GetInstance().Insert(subjectInfo, "SUBJECT");
                this.hdnFlag.Value = "1";
                base.DataEntityViewState = subjectInfo;
                this.txtSubjectID.Text = strSubjectID;
                this.txtSubjectID.Enabled = false;
                this.lblMsg.Text = strSubjectID + base.GetGlobalMsgResource("F9120_005");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9120_006") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.SetSubjectInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetSubjectInfo2Form()
        {
            try
            {
                string strID = base.Request.QueryString["ID"].ToString();
                SubjectInfo subjectInfo = (SubjectInfo) BaseInfoServiceClientProxy.GetInstance().GetInfoByID(strID, "SUBJECT");
                this.txtSubjectID.Text = subjectInfo.SubjectID;
                this.txtSubjectID.Enabled = false;
                this.txtSubjectName.Text = subjectInfo.SubjectName;
                this.txtRemark.Text = subjectInfo.Remark;
                this.chkIsActive.Checked = subjectInfo.IsActive == "Y";
                this.chkSystemCreated.Checked = subjectInfo.SystemCreated == "Y";
                if (subjectInfo != null)
                {
                    base.DataEntityViewState = subjectInfo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateSubjectInfo()
        {
            try
            {
                SubjectInfo subjectInfo = base.DataEntityViewState as SubjectInfo;
                subjectInfo.SubjectName = this.txtSubjectName.Text;
                subjectInfo.Remark = this.txtRemark.Text;
                subjectInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                subjectInfo.SystemCreated = this.chkSystemCreated.Checked ? "Y" : "N";
                subjectInfo.UpdatedBy = base.UserInfo.UpdatedBy;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(subjectInfo, "SUBJECT") + base.GetGlobalMsgResource("F9120_003");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9120_004") + ex.Message;
            }
        }
    }
}

